package com.woniu.project.service.impl;

import com.woniu.project.core.domain.Permission;
import com.woniu.project.core.domain.PermissionType;
import com.woniu.project.core.domain.RolePermission;
import com.woniu.project.dao.PermissionMapper;
import com.woniu.project.dao.PermissionTypeMapper;
import com.woniu.project.dao.RolePermissionMapper;
import com.woniu.project.dto.PermissionDto;
import com.woniu.project.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Klaus
 * @since 2020-10-13
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private PermissionTypeMapper permissionTypeMapper;

    /**
     * 根据角色ID，查询对应的权限数组
     *
     * @param roleId
     * @return
     */
    @Override
    public List<PermissionDto> selectRoleByRoleId(Integer roleId) {
        //根据角色ID查询对应的权限ID数组
        List<RolePermission> rolePermissions = rolePermissionMapper.selectPermissionByRoleId(roleId);

        //根据权限ID查询对应的权限数据
        List<PermissionDto> permissionDtos = new ArrayList<>();
        for (RolePermission rolePermission : rolePermissions) {
            Permission permission = permissionMapper.selectById(rolePermission.getPermissionId());
            PermissionDto permissionDto = new PermissionDto();
            BeanUtils.copyProperties(permission,permissionDto);
            permissionDtos.add(permissionDto);
        }
        return permissionDtos;
    }

    /**
     * 读取excel中的权限配置
     *
     * @return
     */
    @Override
    public void saveExcelInfo() {
        PermissionServiceImpl obj = new PermissionServiceImpl();
        // 此处为我创建Excel路径：E:/zhanhj/studysrc/jxl下
        File file = new File("C:\\Users\\Lucky\\Desktop\\广丰典当项目\\permission.xls");
        try {
            // 创建输入流，读取Excel
            InputStream is = new FileInputStream(file.getAbsolutePath());
            // jxl提供的Workbook类
            Workbook wb = Workbook.getWorkbook(is);
            // Excel的页签数量
            int sheet_size = wb.getNumberOfSheets();
            for (int index = 0; index < sheet_size; index++) {
                // 每个页签创建一个Sheet对象
                Sheet sheet = wb.getSheet(index);

                // sheet.getRows()返回该页的总行数
                for (int i = 0; i < sheet.getRows(); i++) {
                    String permissionName = sheet.getCell(0, i).getContents();
                    String permissionAddr = sheet.getCell(1, i).getContents();
                    String permissionTypeName = sheet.getCell(2, i).getContents();
                    if (i != 0){
                        //判断权限名和权限地址在数据库中是否已经有了
                        Permission permission = permissionMapper.selectPermissionByNameOrAddr(permissionName, permissionAddr);
                        //如果存在相同的权限名或相同的权限地址，打印后跳过
                        if (permission != null){
                            System.out.println("数据库已有相同的权限名或权限地址,不进行任何操作："+permissionName+"---"+permissionAddr);
                            continue;
                        }
                        //如果不存在相同的权限名或相同的权限地址，则判断permissionTypeName 在数据库中是否存在
                        PermissionType permissionType = permissionTypeMapper.selectPermissionTypeByName(permissionTypeName);
                        //如果存在相同的permissionTypeName，则只添加权限表
                        if (permissionType != null){
                            Permission per = new Permission();
                            per.setPermissionName(permissionName);
                            per.setPermissionAddr(permissionAddr);
                            per.setPermissionTypeId(permissionType.getPermissionTypeId());
                            permissionMapper.insert(per);

                            //如果不存在相同的permissionTypeName，则先添加权限表类别表再添加权限表
                        }else {
                            //添加权限类别表
                            PermissionType pertype = new PermissionType();
                            pertype.setPermissionTypeName(permissionTypeName);
                            permissionTypeMapper.insert(pertype);
                            //添加权限表
                            Permission per = new Permission();
                            per.setPermissionName(permissionName);
                            per.setPermissionAddr(permissionAddr);
                            per.setPermissionTypeId(pertype.getPermissionTypeId());
                            permissionMapper.insert(per);
                        }
                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
